package cn.baage.campus.config.security.service.impl;

import cn.baage.campus.config.security.service.WxUserDetailService;
import cn.baage.campus.model.dto.UserRoleDTO;
import cn.baage.campus.service.UserService;
import cn.baage.campus.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Description: 微信 UserDetailService 实现
 * @Author: 八阿哥
 * @url: baage.cn
 * @Version: 1.0
 **/
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class WxUserDetailServiceImpl implements WxUserDetailService {

    private final UserService userService;

    @Override
    public UserDetails loadUserByUsername(String wxId) throws UsernameNotFoundException {
        UserRoleDTO authInfoByWx = userService.getAuthInfoByWx(wxId);
        // 处理微信未绑定的情况 抛出异常 / 新增用户
        if (authInfoByWx.getPassword().isEmpty()) {
            return null;
        }
        return SecurityUtils.user2UserDetail(authInfoByWx);
    }


}
